# Updated by Wei on 20140823
# currently not used

from __future__ import division
from operator import itemgetter, attrgetter
from struct import *
import gc
import math
import os
import random
import sys
import time

def get_results_and_compute_p_at_100_against_unpruned_index():
    # key: trec id
    # value: the # of times this document has been used
    relevant_doc_dict = {}
    input_file_name_3 = "/Users/wc3045963/Desktop/programs_20140412/qrels.gov2.all"
    input_file_handler_3 = open(input_file_name_3,"r")
    for line in input_file_handler_3.readlines():
        line_elements = line.strip().split(" ")
        trec_id = line_elements[2]
        relevance_label = line_elements[3]
        if relevance_label != "0":
            if trec_id not in relevant_doc_dict:
                relevant_doc_dict[trec_id] = 1
            else:
                relevant_doc_dict[trec_id] += 1
    input_file_handler_3.close()
    print "len(relevant_doc_dict):",len(relevant_doc_dict)

    numOfRelevantResults = 0
    numOfResultsReturned = 0
    # key: qID
    # value: list of document candidates
    qIDWithTOP10DocumentResultDict = {}
    inputFileName2 = "/Users/wc3045963/Desktop/programs_20140412/rawResultFile_150_human_queries_top2M_AND_20140412"
    inputFileHanlder2 = open(inputFileName2,"r")
    currentQueryID = ""
    currentLine = inputFileHanlder2.readline()
    while currentLine:
        if currentLine.strip().startswith("qid:"):
            currentLineElements = currentLine.strip().split(" ")
            print currentLineElements
            currentQueryID = currentLineElements[1]
            
            # for debug
            # if currentQueryID == "704":
            #    break
            
            if currentQueryID not in qIDWithTOP10DocumentResultDict:
                qIDWithTOP10DocumentResultDict[currentQueryID] = []

        if currentLine.strip().startswith("Score:"):
            currentLineElements = currentLine.strip().split("\t")
            currentDocIDStringPair = currentLineElements[1]
            currentTrecIDStringPair = currentLineElements[2]
            currentDocID = currentDocIDStringPair.strip().split(":")[1].strip()
            currentTrecID = currentTrecIDStringPair.strip().split(":")[1].strip()
            # print currentDocID,currentTrecID

            if len(qIDWithTOP10DocumentResultDict[currentQueryID]) < 99:
                qIDWithTOP10DocumentResultDict[currentQueryID].append(currentTrecID)
                numOfResultsReturned += 1
                if currentTrecID in relevant_doc_dict:
                    numOfRelevantResults += 1
                else:
                    pass
            else:
                pass

        currentLine = inputFileHanlder2.readline()
    print "len(qIDWithTOP10DocumentResultDict):",len(qIDWithTOP10DocumentResultDict)
    # print "qIDWithTOP10DocumentResultDict:",qIDWithTOP10DocumentResultDict
    inputFileHanlder2.close()
    print "numOfResultsReturned:",numOfResultsReturned
    print "numOfRelevantResults:",numOfRelevantResults

get_results_and_compute_p_at_100_against_unpruned_index()



























